package smedi.ismedi.web.bff.controller.safetyeducation;

import com.smedi.ismedi.file.dto.response.FileInfoResponse;
import com.smedi.ismedi.file.feign.RemoteFileFeignApi;
import com.smedi.ismedi.safety.education.api.dto.request.DailyMorningMeetingQueryRequest;
import com.smedi.ismedi.safety.education.api.dto.request.DailyMorningMeetingUpdateRequest;
import com.smedi.ismedi.safety.education.api.dto.response.DailyMorningMeetingResponse;
import com.smedi.ismedi.safety.education.api.dto.response.PaginationResponse;
import com.smedi.ismedi.safety.education.api.feign.RemoteDailyMorningMeetingFeignApi;
import com.smedi.ismedi.web.starter.common.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import smedi.ismedi.web.bff.controller.BaseController;
import smedi.ismedi.web.bff.infrastructure.util.FileHelper;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 每日晨会BFF
 *
 * @author YangJin
 */
@Slf4j
@RestController
@RequestMapping("/webbff/v1/dailyMorningMeeting")
@RequiredArgsConstructor
@Api(tags = "每日晨会API")
public class DailyMorningMeetingController extends BaseController {
    private final RemoteDailyMorningMeetingFeignApi remoteDailyMorningMeetingFeignApi;
    private final RemoteFileFeignApi fileFeignApi;

    @Value("${fdfs.service.address.in}")
    private String FDFS_SERVICE_ADDRESS_IN;

    @Value("${fdfs.service.address.out}")
    private String FDFS_SERVICE_ADDRESS_OUT;

    /**
     * 创建每日晨会
     *
     * @param dailyMorningMeetingUpdateRequest 每日晨会创建参数
     * @return DailyMorningMeetingResponse
     */
    @ApiOperation(httpMethod = "POST", value = "创建每日晨会")
    @PostMapping(value = "/saveDailyMorningMeeting")
    public ResponseData<DailyMorningMeetingResponse> postSaveDailyMorningMeeting(@Validated @RequestBody DailyMorningMeetingUpdateRequest dailyMorningMeetingUpdateRequest) {
        return remoteDailyMorningMeetingFeignApi.postSaveDailyMorningMeeting(dailyMorningMeetingUpdateRequest);
    }

    /**
     * 删除每日晨会
     *
     * @param educationIds
     * @return
     */
    @ApiOperation(httpMethod = "DELETE", value = "删除每日晨会")
    @DeleteMapping(value = "/deleteDailyMorningMeeting")
    public ResponseData deleteDailyMorningMeeting(@Validated @RequestBody List<Long> educationIds) {
        return remoteDailyMorningMeetingFeignApi.deleteDailyMorningMeeting(educationIds);
    }

    /**
     * 查询每日晨会
     *
     * @param dailyMorningMeetingQueryRequest
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "查询每日晨会")
    @GetMapping(value = "/queryDailyMorningMeeting")
    public ResponseData<PaginationResponse> getQueryAllDailyMorningMeeting(@Validated @SpringQueryMap DailyMorningMeetingQueryRequest dailyMorningMeetingQueryRequest) {
        return remoteDailyMorningMeetingFeignApi.getQueryAllDailyMorningMeeting(dailyMorningMeetingQueryRequest);
    }

    /**
     * 查询单个每日晨会信息
     *
     * @param id
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "查询单个每日晨会信息")
    @GetMapping(value = "/queryDailyMorningMeetingInfo")
    public ResponseData<DailyMorningMeetingResponse> getDailyMorningMeetingInfo(@RequestParam("id") Long id) {
        return remoteDailyMorningMeetingFeignApi.getDailyMorningMeetingInfo(id);
    }

    /**
     * 导出单个每日晨会信息
     *
     * @param id
     * @return
     * @throws IOException
     */
    @ApiOperation(httpMethod = "GET", value = "预览单个每日晨会信息")
    @GetMapping(value = "/previewDailyMorningMeeting")
    public ResponseData<String> previewDailyMorningMeeting(@RequestParam("id") Long id, HttpServletResponse response) throws IOException {
        String signKey = remoteDailyMorningMeetingFeignApi.exportDailyMorningMeeting(id).getData();
        FileInfoResponse fileInfoResponse = fileFeignApi.getFileInfo(signKey).getData();
        String url = FileHelper.getUrl(FDFS_SERVICE_ADDRESS_OUT, fileInfoResponse.getStorageGroup(), fileInfoResponse.getPath(), fileInfoResponse.getName(), response);

        return new ResponseData<>().success(url);
    }

    /**
     * 预览多个每日晨会信息
     *
     * @param ids
     * @return
     * @throws IOException
     */
    @ApiOperation(httpMethod = "POST", value = "预览多个每日晨会信息")
    @PostMapping(value = "/previewDailyMorningMeetingBatch")
    public ResponseData<String> previewDailyMorningMeetingBatch(@Validated @RequestBody List<Long> ids, HttpServletResponse response) {
        String signKey = remoteDailyMorningMeetingFeignApi.exportDailyMorningMeetingBatch(ids).getData();
        FileInfoResponse fileInfoResponse = fileFeignApi.getFileInfo(signKey).getData();
        String url = FileHelper.getUrl(FDFS_SERVICE_ADDRESS_OUT, fileInfoResponse.getStorageGroup(), fileInfoResponse.getPath(), fileInfoResponse.getName(), response);

        return new ResponseData<>().success(url);
    }

    /**
     * 导出单个每日晨会信息
     *
     * @param id
     * @return
     * @throws IOException
     */
    @ApiOperation(httpMethod = "GET", value = "导出单个每日晨会信息")
    @GetMapping(value = "/exportDailyMorningMeeting")
    public ResponseData<Boolean> exportDailyMorningMeeting(@RequestParam("id") Long id, HttpServletResponse response) throws IOException {
        String signKey = remoteDailyMorningMeetingFeignApi.exportDailyMorningMeeting(id).getData();
        FileInfoResponse fileInfoResponse = fileFeignApi.getFileInfo(signKey).getData();
        FileHelper.downloadFile(FDFS_SERVICE_ADDRESS_IN, fileInfoResponse.getStorageGroup(), fileInfoResponse.getPath(), fileInfoResponse.getName(), response);

        return new ResponseData<>().success(true);
    }

    /**
     * 导出多个每日晨会信息
     *
     * @param ids
     * @return
     * @throws IOException
     */
    @ApiOperation(httpMethod = "POST", value = "导出多个每日晨会信息")
    @PostMapping(value = "/exportDailyMorningMeetingBatch")
    public ResponseData<Boolean> exportDailyMorningMeetingBatch(@Validated @RequestBody List<Long> ids, HttpServletResponse response) {
        String signKey = remoteDailyMorningMeetingFeignApi.exportDailyMorningMeetingBatch(ids).getData();
        FileInfoResponse fileInfoResponse = fileFeignApi.getFileInfo(signKey).getData();
        FileHelper.downloadFile(FDFS_SERVICE_ADDRESS_IN, fileInfoResponse.getStorageGroup(), fileInfoResponse.getPath(), fileInfoResponse.getName(), response);

        return new ResponseData<>().success(true);
    }
}
